package org.come.mouslisten;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.math.BigDecimal;
import java.util.Map;

import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;

import org.come.Frame.HuaLianJframe;
import org.come.Frame.TestPetJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.HallucinationJpanel;
import org.come.Jpanel.TestPetJpanel;
import org.come.bean.AllPet;
import org.come.bean.ConfigureBean;
import org.come.entity.RoleSummoning;
import org.come.gltools.RendererTools;
import org.come.model.Configure;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.MessagrFlagUntil;
import org.come.until.SendRoleAndRolesummingUntil;
import org.come.until.UserMessUntil;

import com.tool.Document.RichDocument;
import com.tool.PanelDisplay.PetPanelShow;
import com.tool.role.RoleData;
import org.come.xinyuan.SkillMainFrame5;

public class PetJlistChoseMouslisten implements MouseListener, MouseMotionListener {
	private JList<String> listpet;// 召唤兽列表选中框

	private TestPetJpanel testPetJpanel;// 宠物面板
	private HallucinationJpanel hallucinationJpanel;
	private int ndsl = 4;
	public PetJlistChoseMouslisten(JList<String> listpet, TestPetJpanel testPetJpanel) {
		ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
		Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
		Configure configure = item.get(new BigDecimal(1));

		if(configure.getNdslsx()!=null) {
			ndsl = Integer.parseInt(configure.getNdslsx());
		}
		this.listpet = listpet;
		this.testPetJpanel = testPetJpanel;
	}
	public PetJlistChoseMouslisten(JList<String> listpet, HallucinationJpanel hallucinationJpanel) {
		this.listpet = listpet;
		this.hallucinationJpanel = hallucinationJpanel;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// 打开了窗体
		if (e.isMetaDown()) {// 检测鼠标右键单击
			FormsManagement.HideForm(6);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
		Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
		Configure configure = item.get(new BigDecimal(1));
		// 判断现在是否有召唤兽
		if (UserMessUntil.getPetListTable().size() != 0) {// 有
			RoleSummoning pet = UserMessUntil.getPetListTable().get(listpet.getSelectedIndex());
			// 将选中的召唤兽信息保存到静态中备用
			if (listpet.getSelectedIndex() != -1) {
				UserMessUntil.setChosePetMes(UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()));
			}
			if (e.getButton() == MouseEvent.BUTTON1) {// 左键点击
				if (UserMessUntil.getChosePetMes() != null) {
					RoleSummoning chosePetMes = UserMessUntil.getChosePetMes();
					if(configure.getPethun().equals("开")){
						if(chosePetMes.getRefine()!=null){
							TestPetJframe.getTestPetJframe().getTestPetJpanel().getBtnlh().setVisible(true);
						}else{
							TestPetJframe.getTestPetJframe().getTestPetJpanel().getBtnlh().setVisible(false);
						}
					}else{
						TestPetJframe.getTestPetJframe().getTestPetJpanel().getBtnlh().setVisible(false);
					}

					if (e.isShiftDown()) {
						try {
							JTextField SendMes = ZhuFrame.getZhuJpanel().getSendMes();
							((RichDocument) SendMes.getDocument()).insertRich(SendMes.getCaretPosition(), 3,
									UserMessUntil.getChosePetMes().getSid(), "["
											+ UserMessUntil.getChosePetMes().getSummoningname() + "]", "G", null);
						} catch (BadLocationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					// 判断鼠标是否是加锁图标
					if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE10)) {
						MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
						// 判断是否已加锁
						if (UserMessUntil.getChosePetMes().getPetlock() == 1) {
							ZhuFrame.getZhuJpanel().addPrompt("该召唤兽已加锁");
						} else {
							UserMessUntil.getChosePetMes().setPetlock(1);
							// 发送服务器
							try {
								SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							ZhuFrame.getZhuJpanel().addPrompt("加锁成功");
						}
					}
					// 判断鼠标是否是加锁图标
					if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE11)) {
						// 判断是否已加锁
						if (UserMessUntil.getChosePetMes().getPetlock() == 1) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
							UserMessUntil.getChosePetMes().setPetlock(0);
							// 发送服务器
							try {
								SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							ZhuFrame.getZhuJpanel().addPrompt("解锁成功");
						}
					}
				}
			}

			// 判断选中的召唤兽是不是携带的召唤兽
			if (RoleData.getRoleData().getLoginResult().getSummoning_id() == null||
					(RoleData.getRoleData().getLoginResult().getSummoning_id().compareTo(UserMessUntil.getChosePetMes().getSid()))!= 0) {
				testPetJpanel.getBtnwar().setText("参战");
				testPetJpanel.getBtnwar().setTypeBtn(2);
			} else {
				testPetJpanel.getBtnwar().setText("休息");
				testPetJpanel.getBtnwar().setTypeBtn(3);
			}
			// 将选中的信息展示在面板上
			PetPanelShow.ShowMesForJpanel(false);
			// 等级
			if (listpet.getSelectedIndex() == -1) {
				return;
			}


			PetPanelShow.changePetGrade(UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()).getGrade(),UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()).getSummoningname());
			// 判断这只召唤兽是否有内丹
			if (UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()).getInnerGoods() != null
					&& !"".equals(UserMessUntil.getChosePetMes().getInnerGoods())) {// 有
				String[] strings = UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()).getInnerGoods().split("\\|");
				for (int i = 0; i < ndsl; i++) {//召唤兽内丹
					TestPetJpanel.getLabNedan()[i].setGoodstable(null);
					TestPetJframe.getTestPetJframe().getTestPetJpanel().remove(TestPetJpanel.getLabNedan()[i]);
				}
				if (strings.length > 0) {
					for (int i = 0; i < strings.length; i++) {
						// 将内丹物品放进去监听中
						TestPetJframe.getTestPetJframe().getTestPetJpanel().add(TestPetJpanel.getLabNedan()[i]);
						TestPetJpanel.getLabNedan()[i].setGoodstable(GoodsListFromServerUntil.fushis.get(new BigDecimal(strings[i])));
					}
				}
			} else {// 没有
				for (int i = 0; i < ndsl; i++) {//召唤兽内丹
					TestPetJpanel.getLabNedan()[i].setGoodstable(null);
					TestPetJframe.getTestPetJframe().getTestPetJpanel().remove(TestPetJpanel.getLabNedan()[i]);
				}
			}
//			TestPetJpanel.showStar();
			if (FormsManagement.getframe(18).isVisible()) {
				ChosePetSkillsMouslisten.refreshPetSkills();
			}
			if (FormsManagement.getframe(601).isVisible()) {
				ChosePetLxMouslisten.refreshPetSkills();
			}
			if (FormsManagement.getframe(701).isVisible()) {//葫芦娃
				switch (pet.getSummoningid()) {
					case "754":
					case "755":
					case "756":
					case "757":
					case "758":
					case "759":
					case "760":
						HuaLianJframe.getHuaLianJframe().getHuaLianJpanel().updatePet(pet);
						break;
				}
			}
			if (FormsManagement.getframe(602).isVisible()) {
				// 切换召唤兽时关闭修炼窗口
				FormsManagement.HideForm(602);
			}
			try {
				if (FormsManagement.getframe(688).isVisible()) {
					if("200193".equals(UserMessUntil.getChosePetMes().getSummoningid())){
						SkillMainFrame5.getSkillMainFrame().getSkillMainPanel().copyOfSkillTYCPanel.changeBtnPanel();
					}else{
						FormsManagement.HideForm(688);
					}
				}
			}catch (Exception e1){
				FormsManagement.HideForm(688);
			}
			// 飞升召唤兽展示灵犀按钮
			//灵犀显示按钮
//			if(!configure.getLzjskg().equals("3")) {
			if (UserMessUntil.getPetListTable().get(listpet.getSelectedIndex()).getTurnRount() >= 4) {
				TestPetJframe.getTestPetJframe().getTestPetJpanel().getLingxi().setVisible(true);
			} else {
				TestPetJframe.getTestPetJframe().getTestPetJpanel().getLingxi().setVisible(false);
			}
//			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		ZhuFrame.getZhuJpanel().pettext();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if (e.getY() / 40 < listpet.getModel().getSize()) {
			int idx = listpet.locationToIndex( e.getPoint());
//			屏蔽以下召唤兽数字不会被刷新
//			listpet.setCellRenderer(new RendererTools(idx, new Color(135,135,135,100)));
//			召唤兽列表刷新问题
			ZhuFrame.getZhuJpanel().creatpettext(UserMessUntil.getPetListTable().get(idx));
		}

	}

}
